package cn.chen.controller;

import cn.chen.model.Good;
import cn.chen.service.GoodService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hbpu
 * @since 2023-05-27
 */
@RestController
@RequestMapping("/good")
public class GoodController {
    @Autowired
    private GoodService goodService;

    //发布商品
    @PostMapping ("/publish")
    public boolean publish(@RequestBody Good good){
        //填充发布时间，使用当前时间，格式为yyyy-MM-dd HH:mm:ss
        good.setCreateTime(String.valueOf(LocalDateTime.now()));
        return goodService.save(good);
    }




    //删除商品，根据商品id删除商品
    @GetMapping("/delete")
    public String delete(Good good){
        QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",good.getId());
        Good good1 = goodService.getOne(queryWrapper);
        if(good1==null){
            return "商品不存在";
        }
        goodService.removeById(good);
        return "删除成功";
    }

    //修改商品信息，根据商品id修改商品信息
    @PostMapping ("/update")
    public String update(@RequestBody Good good){
        QueryWrapper<Good> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",good.getId());
        Good good1 = goodService.getOne(queryWrapper);
        if(good1==null){
            return "商品不存在";
        }
        goodService.updateById(good);
        return "修改成功";
    }

    //分页查询商品列表
    @GetMapping("/list")
    public Page<Good> list(int page, int size){
        return goodService.page(new Page<>(page,size));
    }

    //根据商品id查询商品信息
    @GetMapping("/get")
    public Good get(Integer id){
        return goodService.getById(id);
    }






}
